package com.zx.viewlearn.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.zx.viewlearn.R;

/**
 * TODO: document your custom view class.
 */
public class MyView1 extends View {
    public MyView1(Context context) {
        super(context);
    }

    public MyView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    int lastX = 0;
    int lastY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:  //自定义控件，滑动
                int offsetX = x - lastX;
                int offsetY = y - lastY;
                //方式1:layout
//                layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
                //方式2：设置偏移量
//                offsetLeftAndRight(offsetX);
//                offsetTopAndBottom(offsetY)
//              方式三：动画 见ViewLearnActivity1
                //方式4:属性动画
//方式5 scrollBy
                ((View) getParent()).scrollBy(-offsetX, -offsetY);


                break;
            default:
                break;
        }

        return true;//方式1 2 5
//        return super.onTouchEvent(event);//方式3 4
    }
}
